package com.glodon.paas.activity.util;

import java.util.HashSet;
import java.util.Set;

import com.glodon.paas.util.ObjectUtil;

public class NumberUtil {

    public static Long[] long2Array(Long input) {
        if (input == null) {
            input = 0L;
        }
        // 先获取位数
        int size = 0;
        long head = input;
        long tail = 0;
        while (head > 0) {
            tail = head & 1;
            head = head / 2;
            if (tail == 1) {
                size++;
            }
        }

        Long[] rt = new Long[size];

        head = input;
        size = 0;
        int index = 0;
        while (head > 0) {
            tail = head & 1;
            head = head / 2;
            if (tail == 1) {
                rt[size] = tail << index;
                size++;
            }
            index++;
        }
        return rt;
    }

    public static Set<Long> long2Set(Long defaultReceiver) {
        Long[] arr = long2Array(defaultReceiver);
        Set<Long> set = new HashSet<Long>();
        for (Long item : arr) {
            set.add(item);
        }
        return set;
    }

    public static Long set2Long(Set<Long> array) {
        Long target = 0L;
        if (array == null) {
            return target;
        }
        for (long item : array) {
            target |= item;
        }
        return target;
    }

    public static Long array2long(Long[] array) {
        Long target = 0L;
        if (array != null && array.length > 0) {
            for (long item : array) {
                target |= item;
            }
        }
        return target;
    }

    public static void main(String[] args) {
        System.out.println(ObjectUtil.toJson(long2Array(0L)));
        System.out.println(ObjectUtil.toJson(long2Array(1L)));
        System.out.println(ObjectUtil.toJson(long2Array(2L)));
        System.out.println(ObjectUtil.toJson(long2Array(3L)));
        System.out.println(ObjectUtil.toJson(long2Array(5L)));
        System.out.println(ObjectUtil.toJson(long2Array(128L)));
        System.out.println(ObjectUtil.toJson(long2Array(Long.MAX_VALUE)));
        System.out.println(ObjectUtil.toJson(long2Array(Long.MAX_VALUE - 1)));

        long begin = System.currentTimeMillis();
        for (int i = 0; i < 1000000; i++) {
            long2Array(Long.MAX_VALUE - 1);
        }
        long end = System.currentTimeMillis();
        System.out.println(end - begin);
    }
}
